package com.usian.service.impl;

import com.netflix.discovery.converters.Auto;
import com.usian.JsonUtils;
import com.usian.mapper.LocalMessageMapper;
import com.usian.mapper.TbOrderItemMapper;
import com.usian.mapper.TbOrderMapper;
import com.usian.mapper.TbOrderShippingMapper;
import com.usian.mq.MQSender;
import com.usian.pojo.*;
import com.usian.redis.RedisClient;
import com.usian.service.OrderService;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
@Transactional
public class OrderServiceImpl implements OrderService {

    @Value("${ORDER_ID_KEY}")
    private String ORDER_ID_KEY;

    @Value("${ORDER_ID_BEGIN}")
    private Long ORDER_ID_BEGIN;

    @Value("${ORDER_ITEM_ID_KEY}")
    private String ORDER_ITEM_ID_KEY;

    @Autowired
    private RedisClient redisClient;
    @Autowired
    private TbOrderMapper tbOrderMapper;
    @Autowired
    private TbOrderItemMapper tbOrderItemMapper;
    @Autowired
    private TbOrderShippingMapper tbOrderShippingMapper;

    @Autowired
    private LocalMessageMapper localMessageMapper;

    @Autowired
    private AmqpTemplate amqpTemplate;
    @Autowired
    private MQSender mqSender;


    @Override
    public Long insertOrder(OrderInfo orderInfo) {

        //1：tborder
        long timeMillis = System.currentTimeMillis();
        System.out.println("======================"+timeMillis);

        if(!redisClient.exists(ORDER_ID_KEY)){
            redisClient.set(ORDER_ID_KEY,ORDER_ID_BEGIN);
        }
        long orderId = redisClient.incr(ORDER_ID_KEY,5L);
        TbOrder tbOrder = orderInfo.getTbOrder();
        tbOrder.setOrderId(String.valueOf(orderId));
        Date date = new Date();
        tbOrder.setCreateTime(date);
        tbOrder.setUpdateTime(date);
        tbOrder.setStatus(1);
        int num = tbOrderMapper.insertSelective(tbOrder);

        //2:tborder_item
        if(!redisClient.exists(ORDER_ITEM_ID_KEY)){
            redisClient.set(ORDER_ITEM_ID_KEY,ORDER_ID_BEGIN);
        }
        long orderItemId = redisClient.incr(ORDER_ITEM_ID_KEY,5L);
        String orderItemString = orderInfo.getOrderItem();
        List<TbOrderItem> tbOrderItems = JsonUtils.jsonToList(orderItemString, TbOrderItem.class);
        for(TbOrderItem orderItem :tbOrderItems){
            orderItem.setId(String.valueOf(orderItemId));
            orderItem.setOrderId(String.valueOf(orderId));
            int num2 =tbOrderItemMapper.insertSelective(orderItem);
        }

        //3：tborder_shipping
        TbOrderShipping tbOrderShipping = orderInfo.getTbOrderShipping();
        tbOrderShipping.setOrderId(String.valueOf(orderId));
        tbOrderShipping.setCreated(date);
        tbOrderShipping.setUpdated(date);
        int num3 = tbOrderShippingMapper.insertSelective(tbOrderShipping);

        //分布式事务 记录本地消息记录表
        LocalMessage localMessage = new LocalMessage();
        localMessage.setTxNo(UUID.randomUUID().toString());
        localMessage.setOrderNo(String.valueOf(orderId));
        localMessage.setState(0); //0 失败 1 成功
        localMessageMapper.insertSelective(localMessage);


        //发布消息到mq，完成扣减库存
        //amqpTemplate.convertAndSend("order_exchage","order.add", orderId);
        mqSender.sendMsg(localMessage);

        return orderId;
    }
}
